package com;

import com.pojo.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.Reader;
import java.util.List;
public class MyBatisTest {
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;
@Before
    public void init(){
        String resources = "mybatis-config.xml";
        Reader reader = null;
        try {
//            链接数据库
            reader = Resources.getResourceAsReader(resources);
            SqlSessionFactory SqlMappet = new SqlSessionFactoryBuilder().build(reader);
            sqlSession = SqlMappet.openSession();
        }catch (Exception e){

        e.printStackTrace();
        }
    }
@Test
public void findAllStudentTest(){
        List<Student> list = sqlSession.selectList("com.mapper.StudentMapper.findAllStudent");
        for (Student student : list) {
            System.out.println(student);
        }
    }
@After
    public void destory(){
//        提交事务
        sqlSession.commit();
//        关闭数据库
        sqlSession.close();
    }
}
